package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CouponListUserParameter;
import com.cskaoyan.bean.CouponParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.bean.PageParameter;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.model.MarketCoupon;
import com.cskaoyan.model.MarketCouponExample;
import com.cskaoyan.model.MarketCouponUser;
import com.cskaoyan.model.MarketCouponUserExample;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.utils.MyBatisUtil;
import com.cskaoyan.utils.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CoupouServiceImpl implements CouponService {
    @Override
    public PageData querySelective(CouponParameter parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketCouponExample example = new MarketCouponExample();
        example.setOrderByClause(parameter.getSort()+" "+parameter.getOrder());
        MarketCouponExample.Criteria criteria = example.createCriteria();

        //拼接条件
        criteria.andDeletedEqualTo(false);
        if(parameter.getName() != null&& !"".equals(parameter.getName())) {
            criteria.andNameLike("%" + parameter.getName() + "%");
        }
        if (parameter.getType() != null){
            criteria.andTypeEqualTo(parameter.getType());
        }
        if (parameter.getStatus() != null){
            criteria.andStatusEqualTo(parameter.getStatus());
        }

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper couponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        List<MarketCoupon> marketCoupons = couponMapper.selectByExample(example);

        PageData data = PageData.list(marketCoupons);
        sqlSession.close();

        return data;

    }

    @Override
    public void create(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();

        Date date = new Date();
        marketCoupon.setAddTime(date);
        marketCoupon.setDeleted(false);

        MarketCouponMapper couponMapper = sqlSession.getMapper(MarketCouponMapper.class);
        couponMapper.insertSelective(marketCoupon);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void update(Integer orderId) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketCouponUserMapper couponUserMapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        MarketCouponUserExample example = new MarketCouponUserExample();

        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(orderId);

        MarketCouponUser marketCouponUser = new MarketCouponUser();
        marketCouponUser.setStatus((short)0);

        couponUserMapper.updateByExampleSelective(marketCouponUser,example);

    }

    @Override
    public void updateCoupon(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        marketCoupon.setUpdateTime(new Date());

        MarketCouponMapper mapper = sqlSession.getMapper(MarketCouponMapper.class);
        mapper.updateByPrimaryKeySelective(marketCoupon);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteCoupon(MarketCoupon marketCoupon) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponMapper couponMapper = sqlSession.getMapper(MarketCouponMapper.class);

        marketCoupon.setDeleted(true);
        couponMapper.updateByPrimaryKeySelective(marketCoupon);

        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public PageData listuser(PageParameter parameter) {
        CouponListUserParameter listUserParameter=(CouponListUserParameter)parameter;

        PageHelper.startPage(parameter.getPage(),parameter.getLimit());
        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(parameter.getSort()+" "+parameter.getOrder());
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        if (listUserParameter.getUserId()!=null && !"".equals(listUserParameter.getUserId())){
            criteria.andUserIdEqualTo(listUserParameter.getUserId());
        }

        if (listUserParameter.getStatus()!=null && !"".equals(listUserParameter.getStatus())){
            criteria.andStatusEqualTo(listUserParameter.getStatus());
        }

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketCouponUserMapper mapper = sqlSession.getMapper(MarketCouponUserMapper.class);
        List<MarketCouponUser> marketCouponUsers = mapper.selectByExample(example);

        sqlSession.close();

        PageData data = PageData.list(marketCouponUsers);

        return data;
    }

}
